//
//  XLFormViewController+Helper.m
//  iModern
//
//  Created by Ralf on 5/14/15.
//  Copyright (c) 2015 com.starlooking. All rights reserved.
//

#import "XLFormViewController+Helper.h"
#import "iModern-Swift.h"

@implementation XLFormViewController (Helper)

- (void)pushViewController:(UIViewController *)controller
{
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        self.navigationController.interactivePopGestureRecognizer.enabled  = YES;
        self.navigationController.interactivePopGestureRecognizer.delegate = nil;
    }

    [self.navigationController pushViewController:controller animated:YES];
}

- (void)login
{
    [self.view endEditing:YES];
    LoginViewController *controller = [LoginViewController new];
    [self pushViewController:controller];
}

- (void)initBackButton
{
    UIImage         *image          = [UIImage imageNamed:@"Back-Small.png"];
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToPreviousView)];
    self.navigationItem.leftBarButtonItem = backButtonItem;
}

- (void)backToPreviousView
{
    [self.view endEditing:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)alertNetworkConnectionException
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接异常，请检查你的网络设置" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alertView show];
}

- (void)alertUserStatusException
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的帐号可能已经在另一台设备上登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alertView show];
}

- (void)alertErrorMessage:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alertView show];
}

- (void)alertWarningMessage:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alertView show];
}

- (void)alertSuccessMessage:(NSString *)message
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alertView show];
}

- (BOOL)verifyResutSet:(ResultSet *)rs
{
    if (rs == nil)
    {
        //        [self alertNetworkConnectionException];
        [self alertWarningMessage:@"请您填写完整的信息！！！"];
        return NO;
    }

    if (rs.hasErrors)
    {
        if (rs.errorCode == 3020)
        {
            [self alertUserStatusException];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Logout" object:nil];
            [self.navigationController popToRootViewControllerAnimated:YES];
            return NO;
        }
        else
        {
            [self alertWarningMessage:rs.errorMessage];
            return NO;
        }
    }

    return YES;
}

@end
